package com.jsfcompref.solutech.model;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import com.eCommerce.corecontract.dto.ComposantDTO;
import com.jsfcompref.solutech.model.ComposantPanier;
import com.sun.xml.internal.ws.developer.Stateful;

@Stateful
public class PanierAchatBean implements PanierAchatLocal{
	
	private List<ComposantPanier> composantsDuPanier;
	
	//
	
	public PanierAchatBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	@PostConstruct
	public void initialize(){
		composantsDuPanier = new ArrayList<ComposantPanier>();
	}
	
	@PreDestroy
	public void clear(){
		composantsDuPanier = null;
	}
	
	public void ajouterComposant(ComposantDTO composant){
		
		boolean composantFound = false;
		for(ComposantPanier composantPanier:composantsDuPanier){
			if(composantPanier.getComposant().equals(composant)){
				composantPanier.setQuantite(composantPanier.getQuantite()+ 1);
				composantFound = true;
				
			}
			if(!composantFound){
				composantsDuPanier.add(new ComposantPanier(composant,1));
				
			}
		}
	}
	
	public void removeComposant(ComposantDTO composant){
		for(ComposantPanier composantPanier:composantsDuPanier){
			if(composantPanier.getComposant().equals(composant)){
				composantsDuPanier.remove(composantPanier);
				return;
			}
		}
	}
	
	public Double getTotal(){
		if(composantsDuPanier==null || composantsDuPanier.isEmpty())
			return  0.00;
		Double total = 0.00;
		for(ComposantPanier composantPanier:composantsDuPanier){
			total+= (composantPanier.getSubtotal());
		}
		return total;
	}
	
	public void empty(){
		composantsDuPanier.clear();
	}

	public List<ComposantPanier> getComposantsDuPanier() {
		return composantsDuPanier;
	}

	public void setComposantsDuPanier(List<ComposantPanier> composantsDuPanier) {
		this.composantsDuPanier = composantsDuPanier;
	}
	
	
	
	
}
